<?php
//微信公众号开发
namespace Common\Util;

class WechatHtml{
    //获取公众号accesstoken
    public static function getAccessToken(){
        $app_config = BaseData::getAppSetting();
        $stoken = 'wechat_access_token_apppid_' . $app_config['wechat_appid'] . '_' . $app_config['wechat_appsecret'];
        $access_token_s = S($stoken);
        if(!$access_token_s){
            $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$app_config['wechat_appid'].'&secret='.$app_config['wechat_appsecret'];
            $get_token=(array)json_decode(cyz_curl_get_contents($url));
            $access_token = $get_token['access_token'];
            if($access_token){
                S($stoken,$access_token,$get_token['expires_in'] - 300);
                return $access_token;
            }else{
                return false;
            }
        }
        return $access_token_s;
    }
    //获取jsticket
    public static function getJsapiTicket(){
        $app_config = BaseData::getAppSetting();
        $stoken = "wechat_jsapi_ticket_".$app_config['wechat_appid'];
        $jsapi_ticket_s=S($stoken);
        if(!$jsapi_ticket_s)
        {
            $url='https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.self::getAccessToken().'&type=jsapi';
            $output=(array)json_decode(cyz_curl_get_contents($url));
            $jsapi_ticket=$output['ticket'];
            if($jsapi_ticket){
                S($stoken,$jsapi_ticket,$output['expires_in'] - 300);
                return $jsapi_ticket;
            }else{
                return false;
            }
        }
        return $jsapi_ticket_s;
    }

    /**
     * 获取jssdk
     * @return array
     */
    public static function getJssdkData(){
        $app_config = BaseData::getAppSetting();
        $wechat_jsapi_ticket = self::getJsapiTicket();
        $jssdk_url = $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        $jssdk_noncestr = md5(time().rand(111,999));
        $jssdk_timestamp = time();
        $jssdk_str = "jsapi_ticket=".$wechat_jsapi_ticket."&noncestr=".$jssdk_noncestr."&timestamp=".$jssdk_timestamp."&url=".$jssdk_url;
        $jssdk_signature = sha1($jssdk_str);
        $jssdk_param_data = array(
            'appId'=>$app_config['wechat_appid'],
            'timestamp'=>$jssdk_timestamp,
            'nonceStr'=>$jssdk_noncestr,
            'signature'=>$jssdk_signature
        );
        return $jssdk_param_data;
    }

    /***
     * 获取临时二维码
     */
    public static function getWxQrcode($scene_str=''){
        $access_token = self::getAccessToken();
        $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;
        $post_data = array(
            'expire_seconds'=>2592000,
            'action_name'=>'QR_SCENE',
            'action_info'=>array(
                'scene'=>array('scene_id'=>$scene_str)
            )
        );
        $post_data = json_encode($post_data);
        $r_data = cyz_curl_post_contents($url,$post_data);
        $r_data = json_decode($r_data,true);
        return $r_data;
    }

    /**
     * 微信公众号授权登陆
     * @param string $redirect_uri
     */
    public static  function wechatAuth($redirect_uri='',$go_url='')
    {

    }

    /**
     * 验证用户是否已关注公众号
     * @param $openid
     */
    public static function checkFansIsSubscribe($openid){
        $access_token = self::getAccessToken();
        $url='https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
        $output=(array)json_decode(http_request($url));
        return $output;
    }

    /**
     * 发送公众号模板消息通知
     * @param int $user_id
     * @param string $template_id
     * @param array $data
     * @param string $url
     * @return array|bool
     */
    public static function sendWxTemplateMsg($openId='',$template_id='',$url='',$data=array(),$isMpMini=false,$pagepath=''){
        if(empty($openId)) return false;
        $app_config = BaseData::getAppSetting();
        if($isMpMini){
            $msg_arr = array(
                'touser'=>$openId,
                'template_id'=>$template_id,
                'url'=>$url,
                'miniprogram'=>array(
                    'appid'=>$app_config['wechat_appid'],
                    'pagepath'=>$pagepath
                ),
                'data'=>$data
            );
        }else{
            $msg_arr = array(
                'touser'=>$openId,
                'template_id'=>$template_id,
                'url'=>$url,
                'data'=>$data
            );
        }
        $xml=stripslashes(json_encode($msg_arr,JSON_UNESCAPED_UNICODE));
        $http_url='https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.self::getAccessToken();
        return (array)json_decode(http_request($http_url,$xml));
    }
}